package com.xyz.tag.html.element;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.xyz.tag.html.XYHtmlConstants;
import com.xyz.tag.html.XYIAttribute;
import com.xyz.tag.html.XYIElement;
import com.xyz.tag.html.XYIHTMLTag;
import com.xyz.tag.html.attribute.XYAttribute;
import com.xyz.tag.util.StringUtil;


public class XYElement implements XYIElement {

  private static final long serialVersionUID = 2014161164499768349L;
  private String elementName = null;
  private List<XYIHTMLTag> elements = null;
  private Set<XYIAttribute> attributes = null;

  public XYElement( String elementName ) {
    this( elementName, null, null );
  }

  public XYElement( String elementName, Set<XYIAttribute> attributes ) {
    this( elementName, attributes, null );
  }

  public XYElement( String elementName, List<XYIHTMLTag> elements ) {
    this( elementName, null, elements );
  }

  public XYElement( String elementName, Set<XYIAttribute> attributes, List<XYIHTMLTag> elements ) {
    this.elementName = elementName;
    this.attributes = attributes;
    this.elements = elements;
  }

  @Override
  public XYIElement addElement( XYIHTMLTag element ) {
    if ( element != null ) {
      if ( elements == null ) {
        elements = new ArrayList<XYIHTMLTag>();
      }
      elements.add( element );
    }
    return this;
  }

  @Override
  public XYIElement addElements( XYIHTMLTag... elements ) {
    if ( elements != null && elements.length > 0 ) { return addElements( Arrays.asList( elements ) ); }
    return this;
  }

  @Override
  public XYIElement addElements( List<XYIHTMLTag> elements ) {
    if ( elements != null && !elements.isEmpty() ) {
      if ( this.elements == null ) {
        this.elements = new ArrayList<XYIHTMLTag>();
      }
      this.elements.addAll( elements );
    }
    return this;
  }

  @Override
  public XYIElement addAttribute( String name, String value ) {
    return addAttribute( new XYAttribute( name, value ) );
  }

  @Override
  public XYIElement addAttribute( XYIAttribute attribute ) {
    if ( attribute != null ) {
      if ( attributes == null ) {
        attributes = new HashSet<XYIAttribute>();
      }
      attributes.add( attribute );
    }
    return this;
  }

  @Override
  public XYIElement addAttributes( XYIAttribute... attributes ) {
    if ( attributes != null && attributes.length > 0 ) { return addAttributes( new HashSet<XYIAttribute>(
        Arrays.asList( attributes ) ) ); }
    return this;
  }

  @Override
  public XYIElement addAttributes( Set<XYIAttribute> attributes ) {
    if ( attributes != null && !attributes.isEmpty() ) {
      if ( this.attributes == null ) {
        this.attributes = new HashSet<XYIAttribute>();
      }
      this.attributes.addAll( attributes );
    }
    return this;
  }

  @Override
  public XYIElement setElements( XYIHTMLTag... elements ) {
    if ( elements != null && elements.length > 0 ) { return setElements( Arrays.asList( elements ) ); }
    return this;
  }

  @Override
  public XYIElement setElements( List<XYIHTMLTag> elements ) {
    this.elements = elements;
    return this;
  }

  @Override
  public XYIElement setAttributes( Set<XYIAttribute> attributes ) {
    this.attributes = attributes;
    return this;
  }

  @Override
  public void setId( String id ) {
    addAttribute( XYHtmlConstants.attribute_id, id );
  }

  @Override
  public void setClasss( String classs ) {
    addAttribute( XYHtmlConstants.attribute_classs, classs );
  }

  @Override
  public void addClasss( String classs ) {
    appendToAttributeValue( XYHtmlConstants.attribute_classs, classs );
  }

  @Override
  public String draw() {
    StringBuffer html = new StringBuffer();
    html.append( XYHtmlConstants.sign_lt );
    html.append( this.elementName );
    if ( attributes != null && !attributes.isEmpty() ) {
      Iterator<XYIAttribute> iterator = attributes.iterator();
      while ( iterator.hasNext() ) {
        html.append( " " );
        html.append( iterator.next().draw() );
      }
    }
    if ( elements != null && !elements.isEmpty() ) {
      html.append( XYHtmlConstants.sign_gt );
      for ( XYIHTMLTag element : elements ) {
        html.append( element.draw() );
      }
      html.append( XYHtmlConstants.sign_lt ).append( XYHtmlConstants.sign_slash ).append( this.elementName )
          .append( XYHtmlConstants.sign_gt );
    }
    else {
      html.append( XYHtmlConstants.sign_slash + XYHtmlConstants.sign_gt );
    }
    return html.toString();
  }

  protected void appendToAttributeValue( String name, String value ) {
    if ( !StringUtil.isEmpty( name ) && !StringUtil.isEmpty( value ) ) {
      if ( attributes != null && !attributes.isEmpty() ) {
        boolean found = false;
        for ( XYIAttribute attribute : attributes ) {
          if ( attribute.getName() != null && attribute.getName().equalsIgnoreCase( name ) ) {
            attribute.setValue( attribute.getValue() + " " + value );
            found = true;
            break;
          }
        }
        if ( !found ) {
          addAttribute( name, value );
        }
      }
      else {
        addAttribute( name, value );
      }
    }
  }

  @Override
  public void setStyle( String style ) {
    
  }

  @Override
  public void addStyle( String style ) {

  }
}